using System;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeUI;
using Jbe.TestSuite.Infrastructure.Interface;
using Jbe.TestSuite.TestDevice.FunctionGen.Interface.Services;
using Microsoft.Practices.CompositeUI.EventBroker;
using Jbe.TestSuite.TestDevice.FunctionGen.ControlView.Constants;
using Jbe.TestSuite.TestDevice.Manager.Interface.Services;

namespace Jbe.TestSuite.TestDevice.FunctionGen.ControlView.Views
{
    public partial class FunctionGenViewPresenter : Presenter<IFunctionGenView>
    {
        [EventPublication(EventTopicNames.StatusUpdate)]
        public event EventHandler<EventArgs<string>> StatusUpdate;

        private IControlView controlView;
        private IFunctionGen functionGen;

        [ServiceDependency]
        public IControlView ControlView
        {
            set { controlView = value; }
        }

        [ServiceDependency]
        public IFunctionGen FunctionGen
        {
            set { functionGen = value; }
        }

        public override void OnViewReady()
        {
            base.OnViewReady();
            View.Config = functionGen.Config.Clone();
            View.UpdateIsActive(functionGen.IsActive);
        }

        public void OnCloseView()
        {
            base.CloseView();
        }

        public void Start()
        {
            functionGen.Start();
            View.UpdateIsActive(functionGen.IsActive);
            OnStatusUpdate(controlView.Name + " is started.");
        }

        public void Stop()
        {
            functionGen.Stop();
            View.UpdateIsActive(functionGen.IsActive);
            OnStatusUpdate(controlView.Name + " is stopped.");
        }

        [EventSubscription(EventTopicNames.ConfigurationChanged, Thread = ThreadOption.UserInterface)]
        public void ConfigurationChanged(object sender, EventArgs e)
        {
            View.Config = functionGen.Config;
        }

        protected virtual void OnStatusUpdate(string message)
        {
            if (StatusUpdate != null) { StatusUpdate(this, new EventArgs<string>(message)); }
        }
    }
}

